package service

import (
	"context"
	"errors"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/domain/repository"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"
)

type SysRoleService struct {
	Repo repository.SysRoleRepository
}

func NewSysRoleService(repo repository.SysRoleRepository) *SysRoleService {
	return &SysRoleService{Repo: repo}
}

func (s *SysRoleService) Insert(ctx context.Context, dept *model.SysRole) (*model.SysRole, error) {
	return s.Repo.Insert(ctx, dept)
}

func (s *SysRoleService) Update(ctx context.Context, dept *model.SysRole) (int64, error) {
	return s.Repo.Update(ctx, dept)
}

func (s *SysRoleService) UpdateWithOutMenuAndDataScope(ctx context.Context, sysRole *model.SysRole) (int64, error) {
	return s.Repo.UpdateWithOutMenuAndDataScope(ctx, sysRole)
}

func (s *SysRoleService) BatchInsert(ctx context.Context, list []*model.SysRole) ([]int64, error) {
	if len(list) <= 0 {
		return nil, errors.New("系统角色批量新增参数验证失败")
	}
	return s.Repo.BatchInsert(ctx, list)
}

func (s *SysRoleService) SelectById(ctx context.Context, id int64) (*model.SysRole, error) {
	if id == 0 {
		return nil, errors.New("查询参数验证失败")
	}
	return s.Repo.SelectById(ctx, id)
}

func (s *SysRoleService) SelectPage(ctx context.Context, param *request.RolePageParam) ([]*model.SysRole, int64, int64, error) {
	return s.Repo.SelectPage(ctx, param)
}

func (s *SysRoleService) BatchDelete(ctx context.Context, ids []any) (int64, error) {
	if len(ids) <= 0 {
		return 0, errors.New("系统角色批量删除参数验证失败")
	}
	return s.Repo.BatchDelete(ctx, ids)
}

func (s *SysRoleService) SelectList(ctx context.Context, ids []int64) ([]*model.SysRole, error) {
	if len(ids) <= 0 {
		return nil, errors.New("系统角色查询数验证失败")
	}
	return s.Repo.SelectList(ctx, ids)
}

func (s *SysRoleService) SelectIdsByUserId(ctx context.Context, userId any) ([]int64, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.Repo.SelectIdsByUserId(ctx, userId)
}

func (s *SysRoleService) SelectRoleKeyListByUserId(ctx context.Context, userId any) ([]string, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.Repo.SelectRoleKeyListByUserId(ctx, userId)
}

func (s *SysRoleService) SelectRoleNameListByUserId(ctx context.Context, userId any) ([]string, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.Repo.SelectRoleNameListByUserId(ctx, userId)
}

func (s *SysRoleService) SelectAll(ctx context.Context) ([]*model.SysRole, error) {
	return s.Repo.SelectAll(ctx)
}

func (s *SysRoleService) VerifyRoleNameExist(ctx context.Context, role *model.SysRole) (bool, error) {
	sysRole, err := s.Repo.SelectRoleByRoleName(ctx, role)
	if err != nil {
		return false, err
	}
	if sysRole == nil {
		return false, nil
	}
	if sysRole.Id == role.Id {
		return false, err
	}
	return true, nil
}

func (s *SysRoleService) VerifyRoleKeyExist(ctx context.Context, role *model.SysRole) (bool, error) {
	sysRole, err := s.Repo.SelectCountByRoleKey(ctx, role)
	if err != nil {
		return false, err
	}
	if sysRole == nil {
		return false, nil
	}
	if sysRole.Id == role.Id {
		return false, err
	}
	return true, nil
}

func (s *SysRoleService) VerifyRoleIsAllowed(role *model.SysRole) bool {
	if role == nil {
		return false
	}
	if model.IsAdminRole(role.Id) {
		return true
	} else {
		return false
	}
}

func (s *SysRoleService) SelectAllForExcel(ctx context.Context) ([]*model.SysRole, error) {
	return s.Repo.SelectAllForExcel(ctx)
}